package libraryManagerPanels;

 import java.awt.Color;
 import java.awt.Dimension;
 import java.awt.GradientPaint;
import java.awt.event.WindowEvent;
import java.util.HashMap;

import org.jfree.chart.ChartFactory;
 import org.jfree.chart.ChartPanel;
 import org.jfree.chart.JFreeChart;
 import org.jfree.chart.axis.CategoryAxis;
 import org.jfree.chart.axis.CategoryLabelPositions;
 import org.jfree.chart.axis.NumberAxis;
 import org.jfree.chart.plot.CategoryPlot;
 import org.jfree.chart.plot.PlotOrientation;
 import org.jfree.chart.renderer.category.BarRenderer;
 import org.jfree.data.category.CategoryDataset;
 import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;


 /**
 * @author Limor
 * This class create histogram diagram
 */
public class BarChart extends ApplicationFrame {

	
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;



	public BarChart(String title, Double[] pop, Double[] reg) {
       super(title);
        CategoryDataset dataset = createDataset(pop, reg);
        JFreeChart chart = createChart(dataset, title,"Month", "Days");
        ChartPanel chartPanel = new ChartPanel(chart, false);
        chartPanel.setPreferredSize(new Dimension(700, 470));
        setContentPane(chartPanel);
    }
    
    
    
    public BarChart(String title, HashMap<String,Double> x, HashMap<String,Double> y, String[] z ) {
        super(title);
         CategoryDataset dataset = createDataset(x,y,z);
         JFreeChart chart = createChart(dataset, title, "Book Name", "Duration AND Count");
         ChartPanel chartPanel = new ChartPanel(chart, false);
         chartPanel.setPreferredSize(new Dimension(700, 470));
         setContentPane(chartPanel);
     }
    
    

    public void windowClosing(final WindowEvent evt)
    {
    	if(evt.getWindow() == this){
    	dispose();
    	}
    }
    
    
    
    private static CategoryDataset createDataset(Double[] pop, Double[] reg) {
         
        // row keys...
        String series1 = "Popular Book";
        String series2 = "Regular Book";
 
        // column keys...
        String[] month={null, "January","Febuary","March","April","May","June","July","August","September","October","November","December"}; 
       // create the dataset...
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        
        for(int i=1; i<13; i++)
        {
        	dataset.addValue(pop[i], series1, month[i]);
        	dataset.addValue(reg[i], series2, month[i]);
        }
        
       return dataset;     
     }
    

    
   private static JFreeChart createChart(CategoryDataset dataset, String title, String x, String y) {
        
         // create the chart...
      JFreeChart chart = ChartFactory.createBarChart(
            title,       			
            x,             			 
            y,                 		
            dataset,                 	
            PlotOrientation.VERTICAL, 
            true,                   	 
            true,                   	
            false                    
       );

       chart.setBackgroundPaint(Color.white);

       // get a reference to the plot for further customisation...
         CategoryPlot plot = (CategoryPlot) chart.getPlot();
         plot.setBackgroundPaint(Color.lightGray);
         plot.setDomainGridlinePaint(Color.white);
         plot.setDomainGridlinesVisible(true);
         plot.setRangeGridlinePaint(Color.white);
     
        // set the range axis to display integers only...
         final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
         // disable bar outlines...
         BarRenderer renderer = (BarRenderer) plot.getRenderer();
         renderer.setDrawBarOutline(false);
        
         // set up gradient paints for series...
         GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue, 
               0.0f, 0.0f, new Color(0, 0, 64));
         GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green, 
                 0.0f, 0.0f, new Color(0, 64, 0));
         renderer.setSeriesPaint(0, gp0);
         renderer.setSeriesPaint(1, gp1);


         CategoryAxis domainAxis = plot.getDomainAxis();
      domainAxis.setCategoryLabelPositions(
                CategoryLabelPositions.createUpRotationLabelPositions(
                       Math.PI / 6.0));
        // OPTIONAL CUSTOMISATION COMPLETED.
        
         return chart;
     }
     
   
   
 private static CategoryDataset createDataset(HashMap<String,Double> duration, HashMap<String,Double> number, String[] bookName ) {
       
       // row keys...
       String series1 = "Lates Duration";
       String series2 = "Lates Number";

      
      // create the dataset...
       DefaultCategoryDataset dataset = new DefaultCategoryDataset();

       for(int i=0; i<bookName.length; i++)
       {
        dataset.addValue(duration.get(bookName[i]), series1, bookName[i]);
        dataset.addValue(number.get(bookName[i]), series2, bookName[i]);
      
       }
       
      return dataset;
        
    }
   
   
 
 
	 }